home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4828 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: freeing structures
  5. Date: 7 Feb 1996 08:54:41 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4f9pch$9dp@news.iag.net>
  8. References: <4f7913$brg@sepia.nioz.nl>
  9. NNTP-Posting-Host: pm1-orl17.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4f7913$brg@sepia.nioz.nl>, rikko@nioz.nl says...
  13. <snip>
  14. >typedef struct  Line {
  15. >
  16. >        struct  Line  *next;    /* Pointer to next item        */
  17. >        char    **dpart[3];
  18. >        char    **DateLine[3];  /* contains the date in form yy-mm-dd */
  19.  
  20. dpart is defined as an array of 3 pointers to pointer to char
  21. DateLine has a matching definition
  22.  
  23. This is perfectly legal and may be quite valid in your algorithm.  However, 
  24. considering the way you are trying to free your memory, it looks a bit odd.
  25.  
  26. >        char    *RestLine;      /* list of the Values of the line */
  27. >} Line;
  28. >
  29. <snip>
  30. >void ClearLines()
  31. >{
  32. >Line*   line;
  33. <snip>
  34. >                free(line->dpart[0]);
  35.  
  36. Ok you have freed pointer(s) that this pointed to.  Did you need to free the 
  37. char(s) that were pointed to by the pointer(s) first? 
  38.  
  39. >                free(line->dpart[1]);
  40. >                free(line->dpart[2]);
  41. >                free(line->DateLine[0]);
  42. >                free(line->DateLine[1]);
  43. >                free(line->DateLine[2]);
  44. <snip>
  45. >                free(*line->dpart);
  46. >                free(*line->DateLine);
  47.  
  48. *line->dpart is a different form of line->dpart[0] <aka: *(line->dpart + 0)>
  49.  
  50. Ditto *line->DateLine and line->DateLine[0]
  51.  
  52. In both cases you are trying to free pointers that have already beeen freed.
  53.  
  54. >                /*
  55. >                 * delete the line itself and let 'line' point to the next 
  56. line
  57. >                 */
  58. >                free(&line);  /* I suspect this line to trigger the error */
  59. <snip>
  60.  
  61. line is an object of automatic storage class (ie it is automatically 
  62. allocated at the start of the function and destroyed at the end).  By passing
  63. its address to free, you are attempting to free an automatic object.  Not a 
  64. good idea.
  65.  
  66. I suspect you meant to free the dynamic (I hope?) object pointed to by line.
  67. In this case pass the contents of line (a pointer to a Line object) to free.
  68.  
  69.    free(line);
  70.  
  71.  
  72. -- 
  73. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  74. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  75.  
  76.